package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Sfx;
	/**
	 * © 2012 TheJavaArcade.com
	 */	
	public class Music extends Entity {
		public static var sndA1:Sfx = new Sfx(Assets.SND_A1);
		public static var sndA2:Sfx = new Sfx(Assets.SND_A2);
		public static var sndA3:Sfx = new Sfx(Assets.SND_A3);
		public static var sndA4:Sfx = new Sfx(Assets.SND_A4);
		public static var sndA5:Sfx = new Sfx(Assets.SND_A5);
		public static var sndA6:Sfx = new Sfx(Assets.SND_A6);
		public static var sndA7:Sfx = new Sfx(Assets.SND_A7);
		public static var sndAC:Sfx = new Sfx(Assets.SND_AChord);
		public static var sndF1:Sfx = new Sfx(Assets.SND_F1);
		public static var sndF2:Sfx = new Sfx(Assets.SND_F2);
		public static var sndF3:Sfx = new Sfx(Assets.SND_F3);
		public static var sndF4:Sfx = new Sfx(Assets.SND_F4);
		public static var sndF5:Sfx = new Sfx(Assets.SND_F5);
		public static var sndF6:Sfx = new Sfx(Assets.SND_F6);
		public static var sndF7:Sfx = new Sfx(Assets.SND_F7);
		public static var sndFC:Sfx = new Sfx(Assets.SND_FChord);
		
		public var noteNumber:int;	//number 1-7 representing a note on the A Major scale
		public var noteDirection:int;//is the note moving up or down the scale
		public var noteDelay:int;	//number of loops between each note
		public var noteWait:int;		//multiplied by noteCoubt to detemine how long it is before the next note is played
		public var noteCount:int;	//number of notes that have been played since last wall hit; used to decrease volume
		public var noteDivide:int;	//divises noteCount to make for faster shit (coded during champagne saturday morning)
		public var noteChange:int;	//determine when note direction changes
		public var octave:int;		//0 lower octave; 1 higher octave
		
		public var isPlaying:Boolean;	//starts the music playing once a wall has been hit
		
		public function Music() {
			noteCount = 0;
			noteNumber = FP.rand(6)+1;
			noteDelay = 0;
			octave = 0;
			noteDivide = 1;
			isPlaying = false;
		}
		
		public function initMusic():void {//INITMUSIC ==============================================
			if (FP.rand(2) == 1) noteDirection = 1; else noteDirection = -1;
			noteWait = 6 - (Math.abs(GameWorld.ball.x_velocity) + Math.abs(GameWorld.ball.y_velocity));
			if (noteWait < 1) noteWait = 1;
			noteDivide = FP.rand(4) + 1;
			composeMusic();
		}
		public function composeMusic():void {//COMPOSEMUSIC ========================================
			var volume:Number;

			if (noteDelay > (noteCount / noteDivide) * noteWait) {
				noteDelay = -5;
				noteChange--;
				if (noteChange < 1) {
					noteDirection = noteDirection * -1;
					noteChange = FP.rand(3) + 1;
				}
				if (noteCount == 0) {
					noteDirection = noteDirection * -1;
					noteChange = FP.rand(4) + 1;
				}
				noteCount++;
				volume = (15 - (noteCount*1)) / 100;
				noteNumber = noteNumber + noteDirection;
			
				if (noteNumber == 8) noteNumber = FP.rand(6)+1;
				if (noteNumber == 0) noteNumber = FP.rand(6)+1;
			
				if (noteNumber == 1 && octave==0) sndA1.play(volume);
				if (noteNumber == 2 && octave==0) sndA2.play(volume);
				if (noteNumber == 3 && octave==0) sndA3.play(volume);
				if (noteNumber == 4 && octave==0) sndA4.play(volume);
				if (noteNumber == 5 && octave==0) sndA5.play(volume);
				if (noteNumber == 6 && octave==0) sndA6.play(volume);
				if (noteNumber == 7 && octave == 0) sndA7.play(volume);
				
				if (noteNumber == 1 && octave==1) sndF1.play(volume);
				if (noteNumber == 2 && octave==1) sndF2.play(volume);
				if (noteNumber == 3 && octave==1) sndF3.play(volume);
				if (noteNumber == 4 && octave==1) sndF4.play(volume);
				if (noteNumber == 5 && octave==1) sndF5.play(volume);
				if (noteNumber == 6 && octave==1) sndF6.play(volume);
				if (noteNumber == 7 && octave == 1) sndF7.play(volume);
			}
			noteDelay++;
		}
		
		override public function update():void {
			if (isPlaying && !Assets.isMuted) {
				if (noteCount == 0) {
					initMusic();
				}else {
					composeMusic();
				}				
			}

		}
		
	}

}